<?
class clsSearch extends yellowpages{
	var $tw;
	var $tws;
	var $chrlnk;
	var $lnglnk;
 
 function sql(){
  $a = func_get_args();
  if(isset($a[4])){
   if($a[4]>1) 
    $a[4]=($a[4]-1)*10; 
   else
    $a[4]=0;
  }
  $w=""; $chr=""; $tw='';
  if(isset($_REQUEST['chr'])){ $w=" and company_$a[2] like '".$_REQUEST['chr']."%'";}
  if(isset($_REQUEST['tw'])) $tw = " and town.town_id=".$_REQUEST['tw'];
  $o="order by ad DESC, company_$a[2]";
  $s = " SELECT company.company_id, company_$a[1], company_add_$a[1], town_name_$a[1], tel_fax, 
		 prefix, email_address, internet_address, m_phone_no, mdphoneno, map, book, ad, 
		 ad1_image, ad1_link_$a[1], imgfile ";
  switch($a[0]){
   case "search":
    $no='';
    if($a[2]=='vn') $no = yellowpages::checkVn($a[3]);
    $sql="$s
	     FROM company, company_name, no_tel, town 
		 WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		 and (company.town_id=town.town_id) and (company_$a[2]$no like '%$a[3]%') $tw"; 
	$_SESSION['sqlTown']="select company_name.town_id, town_name_$a[1] from company_name, town 
						where (company_name.town_id=town.town_id) and (company_$a[2]$no like '%$a[3]%')
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";
//	echo($sql);
	break;
	
   case "tel":
    $sql="$s
	     FROM company, company_name, no_tel, town 
		 WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		 and (company.town_id=town.town_id) and (tel_fax like '$a[3]%') $tw"; 
	$_SESSION['sqlTown']="select no_tel.town_id, town_name_$a[1] from no_tel, town 
						where (no_tel.town_id=town.town_id) and (tel_fax like '$a[3]%')
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";
	break;
	
   case "comform"://    print_r($a);
   // if(isset($_REQUEST['tw'])) $tw = "and town.town_id=".$_REQUEST['tw'];
    $sql="$s
		 FROM company, company_name, no_tel, town 
		 WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		 and (company.town_id=town.town_id)	and (com_form_id=$a[3]) $tw "; 
	$_SESSION['sqlTown']="select company.town_id,town_name_$a[1] from company, town 
						where (company.town_id=town.town_id) and (com_form_id=$a[3]) $tw
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";
	break;
   
   case 'trade':
   // if(isset($_REQUEST['tw'])) $tw = "and town.town_id=".$_REQUEST['tw'];
    $sql="$s 
		FROM company, company_name, no_tel, town, id_trade
		WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		and (company.town_id=town.town_id) and (company.company_id=id_trade.company_id) 
		and (trade_name_1=$a[3]) $tw ";
	$_SESSION['sqlTown']="select id_trade.town_id,town_name_$a[1] from town, id_trade 
						where (id_trade.town_id=town.town_id) and (trade_name_1=$a[3])
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";
    break;
   case "productList":
    //if(isset($_REQUEST['tw'])) $tw = "and town.town_id=".$_REQUEST['tw'];
    $sql="$s 
		FROM company, company_name, no_tel, town, id_class
		WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		and (company.town_id=town.town_id) and (company.company_id=id_class.company_id) 
		and (class_id_1=$a[3]) $tw ";
	$_SESSION['sqlTown']="select id_class.town_id,town_name_$a[1] from town, id_class 
						where (id_class.town_id=town.town_id) and (class_id_1=$a[3])
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";	 
	break;
   case "head":
    $sql = "select head_$a[2], infone_$a[2], inftwo_$a[2],link_$a[2] from vb_memberprofile, vb_custad_en 
			where vb_memberprofile.memID = vb_custad_en.memID and id=$a[1]";
    break;
  }
// echo("$sql<br>"); Xem source
  return $sql;
 }
 
 function showResult(){//$_SESSION['istype'], $lang, $_SESSION['lng'], $key, $pg, $ids, $tw, $link
  $a = func_get_args(); $i=0; $j=0;$k=0;  $chr="";
 	if(isset($_REQUEST['tw'])) $tw = "&tw=".$_REQUEST['tw'];
 	if(isset($_REQUEST['chr'])) $chr = "&chr=".$_REQUEST['chr'];
  $arr = $this->getName($a[1],'');
  $sql = $this->sql($a[0],$a[1],$a[2],$a[3],$a[4]);// echo($sql);
  if($a[4]>1) $a[4]=(($a[4]-1)*10)+1;
  $result = mysql_query($sql) or die("counld not query");
  $t = "";
  while($rs=mysql_fetch_array($result,MYSQL_NUM)){
   $book='';
   if($rs[11]>0) $book=' class=bgbook';
   $str=""; $adv=""; $k++;
   if(strpos($rs[7],";")>0){ $tmp = explode(";", $rs[7]); $rs[7] = $tmp[0];}
   $rs[12]=(int)$rs[12];
   if($rs[12]>0){ 
	if($i==0){
	 $t.= "<tr bgcolor=#DE7310>
		 <td colspan=2 style=padding-left:3px height=20 class=font12><b>$arr[9]</b></td>
		 <td align=right style=padding-right:3px class=font12>* $arr[12] </td></tr>
		 <tr height=7><td colspan=2></td></tr>";
	 $i++; $j++;
	}
	$adv="<a class=lChar target=_blank href='moredetail$a[7].php?ids=$a[5]$a[6]&pg=$k$chr'>$arr[10]</a>";
   }
   if(($rs[12]==0)&&($i>0)){ 
    $t = substr($t,0,strlen($t)-28);
	$t.= "<tr bgcolor=#DE7310>
		<td colspan=2 style=padding-left:3px height=20 class=font12><b>$arr[11]</b></td>
		<td align=right style=padding-right:3px class=font12>* $arr[12]</td></tr>
		<tr height=7><td colspan=2></td></tr>";
	$i=0; $j++;
   }
   if($rs[13]){ 
    $str="<img class=mrlogo border=0 src=$rs[13]>";
	if($rs[7]) $str="<a target=_blank href='http://$rs[7]'>$str</a><br>";
	else $str="<a target=_blank href='moredetail$a[7].php?ids=$a[5]$a[6]&pg=$k$chr'>$str</a><br>";
   }
   if($a[1]=='cn')
    $str.= "$rs[3], ".str_replace("|","",$rs[2])."<br>";
   else
    $str.= str_replace("|","",$rs[2])." $rs[3] <br>";

   $tel=explode("|",$rs[4]); //print_r($tel);
   $str.="$arr[13] ($rs[5]) $tel[0]<br>";
   if(isset($tel[1])) $str.="$arr[14] ($rs[5]) $tel[1]<br>";
//   echo("$rs[6]<br>");
   if($rs[6]) 
    $rs[6]=" | <a class=lChar href=\"javascript:isopen('feedback$a[7].php?ids=$a[5]&url=$rs[6]&kw=".base64_encode($rs[1])."')\">$arr[15]</a>";
   else
    $rs[6]=" | <a class=lChar href=\"javascript:isopen('feedback$a[7].php?ids=$a[5]&url=&kw=".base64_encode($rs[1])."')\">$arr[15]</a>";
	
   if($rs[7]) $rs[7] = " | <a class=lChar href='http://$rs[7]' title='$rs[7]' target=_black>$arr[16]</a>";
   
   if($rs[10]) $rs[10] = " | <a class=lChar target=_blank href='$rs[10]'>$arr[18]</a>";
   if($rs[14]) $rs[14] = " | <a class=lChar target=_blank href='$rs[14]'>$arr[19]</a>";
   $adv = $adv.$rs[14].$rs[10].$rs[6].$rs[7];
   if(strpos($adv,"|")==1) $adv=substr(trim($adv),2);
   $str.="$adv<script language='javascript'>pro.getPro($rs[0],'$a[1]','$a[5]');</script>";
   $t.="<tr $book><td class=pdr5 height=22>".number_format($a[4]).".</td>
   		<td><a class=".$this->clsAd($rs[12])." target=_blank href=moredetail$a[7].php?ids=$a[5]$a[6]&pg=$a[4]$chr><b>$rs[1]</b></a></td>
   		<td align=right><input name=chk$k type=checkbox value=$rs[0]></td></tr>
		<tr $book><td></td><td>$str</td><td align=right style=padding-left:10px>".$this->getHead($rs[0],$a[1])."</td></tr>
		<tr><td>&nbsp;</td><td colspan=2><hr></td></tr>";
   $a[4]++;
  }
  if($i==0&&$j==0) 
   $t = "<tr bgcolor=#DE7310>
		<td colspan=2 style=padding-left:3px height=20 class=font12><b>$arr[11]</b></td>
		<td align=right style=padding-right:3px class=font12 width=10%>* $arr[12]</td></tr>
		<tr height=7><td colspan=2></td></tr>
		$t";
  return "<table class=tab cellpadding=0 cellspacing=0 width=100%>".$t."</table>";
 }
 
 function getHead($id,$lang){
  //echo($this->sql('head',$id,$lang));
  $result = mysql_query($this->sql('head',$id,$lang)) or die('counld not query');
  $rs = mysql_fetch_array($result,MYSQL_NUM);
  $t='';
  if($rs){
   if($rs[1]) $rs[1]="$rs[1]<br>";
   if($rs[2]) $rs[2]="$rs[2]<br>";
   $t="<table class=tab style='border:#d9d9a4 1px solid' cellpadding=0 cellspacing=0 width=183>
       <tr><td>$rs[0]<br>$rs[1]$rs[2]<a class=head href='http://$rs[3]'>$rs[3]</a></td></tr>
       </table>";
  }
  return $t;
 }
 
 //$_SESSION['sql'],$lang,$_SESSION['lng'],$key,$pg,$lnk,$ids
function getNum(){	//$_SESSION['sql'],$lang,$_SESSION['lng'],$key,$pg, $limit, $lnk, ids
  $a = func_get_args(); $p=array(); //$chrlnk="";// print_r($a);
  $this->lnglnk = $a[1];  
  $lnglnk		= $a[1]; 
  $prev  = "" ;
	if($a[1]=='us'){ 
		$this->lnglnk = '';
		$lnglnk='';
	}
  if(isset($_REQUEST['chr'])&&($_REQUEST['chr'])) $this->chrlnk="&chr=".$_REQUEST['chr'];
  if(isset($_REQUEST['tw'])){ $this->tw=$_REQUEST['tw']; $this->tws="&tw=".$this->tw;}
  $url		= "$a[8]$lnglnk.php?ids=$a[7]";
  $sql		= $a[0];// echo("<br>$a[0]<br>");
  $result	= mysql_query($sql) or die('counld not query num rows');
  $num 		= mysql_num_rows($result); $_SESSION['total']=$num;
  mysql_free_result($result);
  
  $result	= mysql_query($_SESSION['sqlchr']) or die('counld not query num rows');
  $alpha	= $this->getAlpha($result,$url, $a[1]);
  mysql_free_result($result);
  $lg=$this->getName($a[1],$url.$this->tws);
  $pgNum = $num/$a[5];
  if(is_float($pgNum)) $pgNum = floor($pgNum)+1;
  if($a[1]=='cn'){
   $lg[6] .= $this->getTown($lg[8], $this->tw, $lg[17],$a[7]);
  }
  
  if($a[4]==1){
   $bg = 0; $end = $a[5]; $next = "<img border=0 src=images/next.gif>";
   $lnk="<a class=lnext href=$url&pg=2".$this->chrlnk.$this->tws.">$lg[1]</a>";
   $numend=$a[4]*$a[5];
   $btnext = "<a href=$url&pg=".($a[4]+1).$this->chrlnk.$this->tws.">$next</a>";   
   if($pgNum>10) $pgNum=$numend;
   if($pgNum==1) $btnext = "";
   if($num<=10){ 
    $numend=$num; $next=""; $lnk="";
   }
   $p[0] = "<table class=tab cellpadding=0 cellspacing=0 width=100%>
   		   <tr><td><img src=images/next.gif> $lg[5] $a[6] </td>
   		   <td align=right>$lg[2] 1 - $numend $lg[3] ".number_format($num)." $next $lnk</td></tr></table>";
   $p[1] = "<table class=tab cellpadding=0 cellspacing=0 width=100%><tr>
   			<tr><td><img src=images/next.gif> $lg[6] </td>
			<td align=right>$lg[4] ".$this->getPage($bg,$pgNum,$a[8],$lnglnk,$a[7],$a[4])."$btnext</td></tr>
			</table>";
  }
  
  if(($a[4]>1)&&($a[4]<=$pgNum)){
   $pg = $a[4]/$a[5]; $bg = 0; $end = $a[5]; 
   
   if($pg>1){
    if(is_int($pg)) $pg = $pg-1;
    $tmp=floor($pg)*$a[5];
    $bg=$tmp; $end=$tmp+$a[5]; 
   }
   $numend=$a[4]*$a[5];
   $next="<a href=$url&pg=".($a[4]+1).$this->chrlnk.$this->tws."><img border=0 src=images/next.gif></a>";
   $prev = "<a href=$url&pg=".($a[4]-1).$this->chrlnk.$this->tws."><img border=0 src=images/back.gif></a>";
   $btnext = "<img src=images/next.gif><a class=lnext href=$url&pg=".($a[4]+1).$this->chrlnk.$this->tws."> $lg[1]</a>";
   if($a[4]>$a[5])	$numend = $a[4]*$a[5];
   if($a[4]==$pgNum){ $next=""; $btnext='';}
   if($pgNum<$end){ $end=$pgNum;}
   if($num<$numend){$numend=$num; $btnext=""; $next="";}
   $p[0] = "<table class=tab cellpadding=0 cellspacing=0 width=100%><tr><td><img src=images/next.gif> $lg[5] $a[6] </td>
   		   <td align=right><a class=lnext href=$url&pg=".($a[4]-1).$this->chrlnk.$this->tws.">$lg[0] </a> 
		   <img src=images/back.gif> $lg[2] ".number_format(($a[4]-1)*$a[5]+1)." - ".number_format($numend)." $lg[3] ".number_format($num)." $btnext</td></tr></table>";
   $p[1] = "<table class=tab cellpadding=0 cellspacing=0 width=100%><tr>
   			<tr><td><img src=images/next.gif> $lg[6] </td>
			<td align=right>$prev $lg[4] ".$this->getPage($bg,$end,$a[8],$lnglnk,$a[7],$a[4])." $next</td></tr>
			</table>";
  }
  $chr=""; //print_r($alpha);
  if(is_array($alpha)){
   for($i=0;$i<count($alpha);$i++) $chr.=" $alpha[$i]";
   $btnext = $this->getTown($lg[8], $this->tw, $lg[17],$a[7]);
  }else{
   $lg[7]='';
   $btnext='';
  }
  $chr = "<table class=tab cellpadding=0 cellspacing=0 width=100%><tr>
   		<td>$lg[7] $chr</td>
		<td align=right>$btnext</td></tr></table>";
  return "$p[0]||$p[1]||$chr||".number_format($num);
 }
 
 function getAlpha(){//echo($this->tw);
  $a = func_get_args();
  if($a[2]=='cn') return '';
  $t = array("0-9","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", 
  			  "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
  $str=''; $num=''; $k=0;
  //if($this->tw) $tws = "&tw=".$this->tw;
  while($rs=mysql_fetch_array($a[0])){
   $rs[1] = substr($rs[1],0,1); 
   if((is_numeric($rs[1]))&&($k==0)){ 
    $t[0] = "<a class=nopage href=$a[1]".$this->tws."&chr=no>0-9</a>"; $k++;
   }
   for($i=1; $i<count($t); $i++)
    if($rs[1]==$t[$i]) $t[$i]= "<a class=nopage href=$a[1]&chr=$t[$i]".$this->tws.">$t[$i]</a>";
  }
  return $t;
 }
 
 function getPage(){// $bg,$end,$a[8],$lnglnk,$a[7],$a[4],$chrlnk
  $a = func_get_args();// print_r($a);
  $t='';
  for($i=$a[0];$i<$a[1];$i++){
   $tmp=$i+1;
   if($tmp==$a[5])
    $t.="<a class=page1 >$tmp<a>";
   else
    $t.="<a class=nopage href=$a[2]$a[3].php?pg=$tmp&ids=$a[4]".$this->chrlnk.$this->tws.">$tmp</a> ";
  }
  return $t;
 }
 
 function getName(){
  $a = func_get_args(); $t=array();
  switch($a[0]){
   case 'us':
    $t[0]='Prev'; $t[1]='Next'; $t[2]='Listings'; $t[3]='of'; $t[4]='Page'; $t[5]='Language:'; 
	$t[6]="Sort: <a class=nopage href=$a[1]>Alphabetically</a>"; $t[7]='<img src=images/next.gif> Go to:'; 
	$t[8]='Sort by Province';$t[9] = "Yellow Pages - Advertisers"; $t[10]="business profile"; $t[11]="Business Listings"; 
	$t[]="Print"; $t[]="Tel:"; $t[]="Fax:"; $t[]="email"; $t[]="website"; $t[]='(All Province)'; $t[] = 'map'; $t[]='product & service';
	break;
   case 'vn':
    $t[]='Trước'; $t[]='Tiếp'; $t[]='Hiển Thị'; $t[]='trong'; $t[]='Trang'; $t[]='Ngôn ngữ:';
	$t[]="Sắp xếp: <a class=nopage href=$a[1]>Tất cả tỉnh/thành</a>"; $t[]='<img src=images/next.gif> Lọc theo tên:'; $t[]='Tìm Theo Khu Vực';
	$t[] = "Doanh Nghiệp Quảng Cáo"; $t[]="hồ sơ công ty"; $t[]="Liệt Kê Các Doanh Nghiệp"; $t[]="In";
	$t[]="Điện thoại:"; $t[]="Fax:"; $t[]="gửi yêu cầu"; $t[]="website"; $t[]='(Tất cả)'; $t[] = 'bản đồ'; $t[]='sp & dv';
	break;
   case 'cn':
    $t[]='上一页'; $t[]='下一页'; $t[]='本页显示'; $t[]=' , 个共'; $t[]='页数'; $t[]='语言:';
	$t[]="地区: "; $t[]='<img src=images/next.gif> 跳到:'; $t[]='各省类';
	$t[] = "广告服务"; $t[]="公司详情"; $t[]="商业目录"; $t[]="打印";
	$t[]="电话:"; $t[]="传真:"; $t[]="邮件"; $t[]="网址"; $t[]='全省'; $t[] = '地图'; $t[]='服务与产品';
	break;
  }
  return $t;
 }
 
 function clsAd($a){
  switch($a){
   case 9:
    $t = "colCom"; break;
   case 2:
    $t = "colCom1"; break;
   case 1:
    $t = "colCom2"; break;
   default:
    $t = "colCom3";
  }
  return $t;
 }
 
 function getTown(){//$lang, 
  $a = func_get_args();// print_r($a);
  $sql=$_SESSION['sqlTown'];
  $result = mysql_query($sql) or die("counld not query");
  $t="<select id=sltTown name=sltTown class=tab onchange=\"goPages(this.options[this.selectedIndex].value,'$a[3]','$a[1]','".$this->lnglnk."')\">
      	  <option value=0>$a[2]</option>";
  while($rs = mysql_fetch_array($result,MYSQL_NUM)){
   $t.="<option value=$rs[0] ".yellowpages::isCheck($rs[0],$a[1]).">$rs[1]</option>";
  }
  return $t.'</select>';
 }
 
 function getLink(){//$istype, $lang, $ids, $_SESSION['word'], $_SESSION['keyword']
  $a = func_get_args();//  print_r($a);
  $t='';
  if($a[1]=='us') $a[1]='';
  switch($a[0]){
   case 'trade':
    $t= "<a class=toplnk href=tradesearch$a[1].php?ids=$a[2]>".base64_decode(str_replace(' ','+',$a[3]))."</a>"; break;
   case 'productList':
    $t = "<a id=pos class=toplnk href='product$a[1].php?ids=$a[2]'>".base64_decode(str_replace(' ','+',$a[3]))."</a>"; break;
   case 'comform':
    $t = "<a id=lnkcomform class=toplnk href=comform_town$a[1].php?ids=$a[2]&id=$a[4]&word=$a[3]>".base64_decode(str_replace(' ','+',$a[3]))."</a>"; break;
   default:
    $t = "<a class=toplnk href=product$a[1].php?ids=$a[2]>$a[4]</a>";
   
  }
  return $t;
 }
}//end class
?>